package com.example.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Setting;
import com.example.springboot.entity.UWord;
import com.example.springboot.entity.Word;
import com.example.springboot.entity.WordR;
import com.example.springboot.mapper.UWordMapper;
import com.example.springboot.mapper.WordMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/memorizeWord")
public class MemorizeContronller {
    @Resource
    WordMapper wordMapper;

    @Resource
    UWordMapper uWordMapper;


//        int maxid = 5000;
    //@RequestBody Setting setting
    @GetMapping("/task")
    public Result<?> getTask(@RequestParam(defaultValue = "100") Integer taskNumber,
                             @RequestParam(defaultValue = "25") Integer proportion)
    {
        System.out.println(taskNumber);
        System.out.println(proportion);
        //生成设置数目个的不重复的单词的id，并放入记忆任务列表中
        int number = taskNumber;
        QueryWrapper<Word> wrapper = new QueryWrapper();
        int maxid = wordMapper.selectCount(wrapper);
        int[] arr = new int[number];//产生一个能存到number个元素的数组
        //为了防止默认0被当做检查重复的对象所以全部设为-1
        Arrays.fill(arr, -1);
        int index = 0;
        while(index<arr.length){
            Random rd = new Random();
            int num = rd.nextInt(maxid+1);//生成0~10之间的随机数
            if(judge(arr,num)){//judge自定义方法用来判断生成的值是否为数组内重复的元素 如果是 则重新生成
                arr[index++]=num;//如果数组中没有重复的元素就放入数组内 并且索引值++
            }
        }
        List<Word> list = new ArrayList<>();
        for (int i = 0; i < arr.length; i++) {
            Word word = wordMapper.selectById(arr[i]);
            list.add(word);
        }
        return Result.success(list);
    }

    @PostMapping("/updateLevel")
    public Result<?> updateLevel(@RequestBody List<WordR> list) {
        System.out.println("本次数据量"+list.size());
        int have = 0;
        int nohave = 0;
        for (WordR w : list) {
            QueryWrapper<UWord> wrapper = new QueryWrapper();
            wrapper.eq("wid",w.getWid());
            UWord queryUWord = uWordMapper.selectOne(wrapper);
            if (null == queryUWord) {//
                nohave++;
                UWord uWord = new UWord();
                uWord.setWid(w.getWid());
                uWord.setLevel(w.getLevel());
                uWordMapper.insert(uWord);
            } else { //查询到了
                have++;
                Integer level = queryUWord.getLevel() + w.getLevel();
                queryUWord.setLevel(level);
                uWordMapper.updateById(queryUWord);
            }
        }
        System.out.println("have:"+have);
        System.out.println("nohave"+nohave);
        return Result.success();
    }


    public static boolean judge(int[] arr,int num){
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]==num){
                return false;
            }
        }
        return true;
    }


}
